﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using UnityEngine.UI;

public class MainMenuPanel : BasePanel
{
    private CanvasGroup canvasGroup;
    private int timer;//0是默认,1是newgame,2是loadgame
    private float countTime;
    private void Awake()
    {
        //查找是否有存档，如果没有，将禁用继续游戏按钮
        if (!SaveManager.Instance.HasSaveFile())
        {
            CanvasGroup cg = transform.Find("LoadGame").GetComponent<CanvasGroup>();
            cg.interactable = false;
            cg.blocksRaycasts = false;
            transform.Find("LoadGame").Find("Text").GetComponent<Text>().color = new Color32(100, 100, 100, 100);
        }
        timer = 0;
        countTime = 3f;
    }
    public override void OnEnter()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();
    }

    public override void OnExit()
    {

    }

    public override void OnPause()
    {
        if(canvasGroup==null)
            canvasGroup = this.GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = false;
        canvasGroup.interactable = false;
    }

    public override void OnResume()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = true;
        canvasGroup.interactable = true;
    }

    public void NewGameButton() //新游戏
    {
        timer = 1;
        transform.Find("Animator").GetComponent<Animator>().SetTrigger("Enter");
        EffectObjectManager.Instance.ButtonEffects();
    }
    public void LoadGameButton() //继续游戏
    {
        timer = 2;
        transform.Find("Animator").GetComponent<Animator>().SetTrigger("Enter");
        EffectObjectManager.Instance.ButtonEffects();
    }

    public void ExitButton() //退出游戏
    {
        EffectObjectManager.Instance.ButtonEffects();
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #else
            Application.Quit();
        #endif
    }
    public void OptionButton() //选项按钮
    {
        EffectObjectManager.Instance.ButtonEffects();
        UIManager.Instance.PushPanel("OptionPanel");
    }
    private void NewGame()
    {
        ConstOfMap.loadgame = false;
        GameManager.Instance.ChangeState(StateOP.Push, GameState.Running);
    }
    private void LoadGame()
    {
        SaveManager.Instance.LoadGame();
        GameManager.Instance.ChangeState(StateOP.Push, GameState.Running);
    }
    private void Update()
    {
        if(timer != 0)
        {
            countTime -= Time.unscaledDeltaTime;
            if(countTime<=1)
            {
                canvasGroup.alpha -= Time.unscaledDeltaTime;
                if (countTime<0 && canvasGroup.alpha==0)
                {
                    if (timer == 1)
                        NewGame();
                    if (timer == 2)
                        LoadGame();
                }
            }
        }
    }
}
